import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress"; //进度条
import "nprogress/nprogress.css";

NProgress.configure({
  showSpinner: false, //通过将其设置为 false 来关闭加载微调器。
});

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      meta: { title: "登录" },
      component: () => import("@/views/login/login.vue"),
    },
    {
      path: "/register",
      name: "register",
      meta: { title: "注册" },
      component: () => import("@/views/login/register.vue"),
    },
    {
      path: "/sys",
      name: "sys",
      meta: { title: "系统首页" },
      component: () => import("@/views/sys/sys.vue"),
      children: [
        {
          path: "main",
          name: "main",
          meta: { title: "系统资源" },
          component: () => import("@/views/main/main.vue"),
        },
      ],
    },

    //个人项目与组件管理-个人用户
    {
      path: "/user",
      name: "user",
      meta: { title: "用户" },
      component: () => import("@/views/sys/sys.vue"),
      children: [
        {
          path: "personal",
          name: "personal",
          meta: { title: "个人项目与组件管理" },
          component: () => import("@/views/personal/personal.vue"),
        },
      ],
    },

        //风险报告-审计
    {
      path: "/man",
      name: "man",
      meta: { title: "用户" },
      component: () => import("@/views/sys/sys.vue"),
      children: [
        {
          path: "admin",
          name: "admin",
          meta: { title: "风险报告" },
          component: () => import("@/views/admin/admin.vue"),
        },
      ],
    },

    //项目或组件上传
    {
      path: "/up",
      name: "up",
      meta: { title: "项目或组件上传" },
      component: () => import("@/views/sys/sys.vue"),
      children: [
        {
          path: "upload",
          name: "upload",
          meta: { title: "项目或组件上传" },
          component: () => import("@/views/upload/upload.vue"),
        },
      ],
    },

    // 项目或组件下载
    {
      path: "/down",
      name: "down",
      meta: { title: "项目或组件下载" },
      component: () => import("@/views/sys/sys.vue"),
      children: [
        {
          path: "download",
          name: "download",
          meta: { title: "项目或组件下载" },
          component: () => import("@/views/download/download.vue"),
        },
      ],
    },

  ],
});

import defaultSettings from "@/settings";

//路由全局前置钩子
router.beforeEach((to, from, next) => {
  NProgress.start();
  document.title = `${to.meta.title || "可信软件供应链系统"} - ${defaultSettings.title}`;
  let token = localStorage.getItem("token");
  if (token) {
    next();
  } else {
    if (to.path == "/login" || to.path == "/register"  ) {
      next();
    } else {
      next({
        path: "/login",
      });
    }
  }
});

// //路由全局后置钩子
router.afterEach(() => {
  NProgress.done();
});

export default router;
